package First;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 长风无言
 */
public class StudentFunctionImplements implements AddStudents,DeleteStudents,FindStudents,ModifyStudents,ForStudents {

    private List<Student> students = new ArrayList<>();

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public List<Student> getStudents() {
        return students;
    }

    @Override
    public void addStudent(String name, int num, int age) throws StudentIdException, AgeException {
        //添加学生信息
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else if(age < 0) {
            throw new AgeException("请输入大于0的年龄：");
        }else{
            students.add(new Student(name, num, age));
            System.out.println("添加成功");
        }
    }

    @Override
    public boolean deleteStudents(int num) throws StudentIdException {
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else {
            Iterator<Student> iterator = students.iterator();
            // 判断是否还有迭代对象
            while (iterator.hasNext()) {
                // 获取学生对象
                Student student = iterator.next();
                // 判断学生对象的学号和输入的学号是否相同
                if (student.getNum() == num) {
                    iterator.remove();
                    System.out.println("删除成功");
                    return true;
                }
            }
        }
        System.out.println("删除失败，查无此人");
        return false;
    }

    @Override
    public void findStudents(int num) throws StudentIdException {
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else {
            for (Student student : students) {
                // 判断学生对象中的姓名和查找的姓名是否相同
                if (num == student.getNum()) {
                    System.out.println(student.toString());
                }
            }
        }
        System.out.println("查无此人");
    }

    @Override
    public Student modifyStudents(int num,String name, int newnum, int age) throws StudentIdException, AgeException {
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else if(newnum < 0){
                throw new StudentIdException("请输入合理的新学号");
            }else if(age < 0){
                throw new AgeException("请输入合理的学号");
            }else {
                for (Student student : students) {
                    // 判断学生对象的学号是否为要修改信息的学生的雪蛤
                    if (student.getNum() == num) {
                        student.setNum(newnum);
                        student.setName(name);
                        student.setAge(age);
                        System.out.println("修改成功");
                        return student;
                    }
                }
            }
        // 修改失败，无此学生
        System.out.println("修改失败，查无此人");
        return null;
    }

    @Override
    public void forStudent() {
        //遍历每一个学生信息
        for(Student student:students){
            //打印该学生信息
            System.out.println(student.toString());
        }
    }
}
